/**
  @file ait_bsp.c
  @brief It contains the BSP dependent configurations need to be ported to the customer platform.
  @author Philip

  @version
- 1.0 Original version
 */
#include "config_fw.h"
#include "mmp_lib.h"
#include "mmph_hif.h"
#include "ait_bsp.h"
//#include "ait_utility.h"
#if defined(BUILD_CE)
#include "ucos_ii.h"
#include "os_cpu.h"
#include "os_cfg.h"
#endif

MMP_ULONG RTNA_CPU_CLK_M = 0;
//==============================================================================
//                              FUNCTION PROTOTYPES
//==============================================================================
#if PCAM_EN==0
//#pragma O0
/** @brief Set the delay in milli-second in Basebnad.
*/
void	MMPC_System_WaitCount(MMP_ULONG count) 
{
    while (count--);
}

/** @brief Set the delay in milli-second in Basebnad.
*/
void	MMPC_System_WaitMs(MMP_ULONG ulMs)
{
	//MMPC_System_WaitCount((HOST_CLK_M / HOST_WHILE_CYCLE) * ulMs * 1000);
	MMPC_System_WaitCount((HOST_CLK_M * ulMs * 1000) / HOST_WHILE_CYCLE);
}

/** @brief Set the delay in micro-second in Basebnad.
*/
void	MMPC_System_WaitUs(MMP_ULONG ulUs)
{
	//MMPC_System_WaitCount((HOST_CLK_M / HOST_WHILE_CYCLE) * ulUs);
	MMPC_System_WaitCount((HOST_CLK_M  * ulUs) / HOST_WHILE_CYCLE);
}

#define MMPC_TIMEOUT_COUNTER_MAX  (10000)
MMP_ERR MMPC_System_CheckMMPStatus(MMP_ULONG ulCheckflag)
{
	MMP_ULONG	status;
	MMP_ULONG	counter;
	
	counter = 0;
	status = MMPH_HIF_CmdGetStatusL() & ulCheckflag;
	
	while (status && (counter < MMPC_TIMEOUT_COUNTER_MAX)) {
		MMPC_System_WaitMs(1); // using os wait instead of busy wait
		status = MMPH_HIF_CmdGetStatusL() & ulCheckflag;
		counter++;
	}		
	
	if (counter >= MMPC_TIMEOUT_COUNTER_MAX) {
        #if 1
        PRINTF("Time Out Error, staus = %x\n", status);
        #endif
        return MMP_SYSTEM_ERR_CMDTIMEOUT;
    }
    else {
    	return MMP_ERR_NONE;
    }
}
#endif
